$(function(){
	
	getProvinceData();
	province2Change();
});


function add_province(){
	var name=$('#province').val();
	if(name==null || name==''){
		alert("省份名称不能为空");
	}else{
		$.post(rootPath+"/json/addProvince.action",{provinceName:name},function(data){
			getProvinceData();
			$("#city1").html("");
			$('<option value ="">请选择城市</option>').appendTo("#city1");
			alert(data.result);
		},
		"json");
	}
}
function add_city(){
	var province1_id=$("#province1").val();
	var city=$("#city").val();
	if(province1_id==null || province1_id==''){
		alert("添加城市必须选择省份！");
	}else if(city==null || city==''){
		alert("城市名称不能为空");
	}else{
		$.post(rootPath+"/json/addCity.action",{provinceId:province1_id,cityName:city},function(data){
			alert(data.result);
		},
		"json");
	}
}
function add_school(){
	var city_id = $("#city1").val();
	var schoolName=$("#school").val();
	if(city_id==null || city_id==''){
		alert("添加学校必须选择城市！");
	}else if(schoolName==null || schoolName==''){
		alert("学校名称不能为空");
	}else{
		$.post(rootPath+"/json/addSchool.action",{cityId:city_id,schoolName:schoolName},function(data){
			alert(data.result);
		},
		"json");
	}
	
}
function getProvinceData(){
	$('#province1').html("");
	$('#province2').html("");
	$('<option value ="">请选择省份</option>').appendTo($('#province1'));
	$('<option value ="">请选择省份</option>').appendTo($('#province2'));
	$.post(rootPath+"/json/acquireAllProvince.action",{},function(data){
		for(var i=0;i<data.list.length;i++){
			$('<option value="'+data.list[i].id+'">'+data.list[i].name+'</option>').appendTo($('#province1'));
			$('<option value="'+data.list[i].id+'">'+data.list[i].name+'</option>').appendTo($('#province2'));
		}
	},
	"json");
}
function province2Change(){
	$("#province2").change(function(){
			var this_value=$(this).val();
			if(this_value){
				$("#city1").html("");
				$('<option value ="">请选择城市</option>').appendTo("#city1");
				$.post(rootPath+"/json/acquirtCityByProvinceId.action",
						{provinceId:this_value},
						function(data){
							if(data.list.length>0){
								for(var i=0;i<data.list.length;i++){
									$('<option value="'+data.list[i].id+'">'+data.list[i].name+'</option>').appendTo($('#city1'));
								}
							}else{
								alert("本省中暂无城市信息");
							}
						},
				"json");
			}else{
				$("#city1").html("");
				$('<option value ="">请选择城市</option>').appendTo("#city1");
			}
	});
}
